#!/bin/zsh

### Create a cross-platform temporary directory/file for antidote.
#
# usage: __antidote_mktemp [-d] [-f suffix]
#   -d        Create a directory rather than a file
#   -s        Use this for the temp file/dir
#
# Returns the path of created temp directory/file.
#
#function __antidote_mktemp {
emulate -L zsh; setopt local_options $_adote_funcopts

local -a o_dir o_suffix
zparseopts $_adote_zparopt_flags -- d=o_dir s:=o_suffix

# Set the appropriate temp directory (cargo cult code from p10k)
local tmpbase
if [[ -n "$TMPDIR" && (( -d "$TMPDIR" && -w "$TMPDIR" ) || ! ( -d /tmp && -w /tmp )) ]]; then
  tmpbase="${TMPDIR%/}"
else
  tmpbase="/tmp"
fi

# Create the pattern with PID
local pattern="antidote.$$"

# Add suffix if provided with -s
if (( $#o_suffix )) && [[ -n "${o_suffix[-1]}" ]]; then
  pattern="${pattern}.${o_suffix[-1]}"
fi

# Add random chars
pattern="${pattern}.XXXXXXXXXX"

# Create temp directory or file
if (( $#o_dir )); then
  command mktemp -d "${tmpbase}/${pattern}"
else
  command mktemp "${tmpbase}/${pattern}"
fi
#}
